package com.sz.admin.sysuseremployee.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.sysuseremployee.service.SysUserEmployeeService;
import com.sz.admin.sysuseremployee.pojo.po.SysUserEmployee;
import com.sz.admin.sysuseremployee.mapper.SysUserEmployeeMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.sz.admin.sysuseremployee.pojo.dto.SysUserEmployeeCreateDTO;
import com.sz.admin.sysuseremployee.pojo.dto.SysUserEmployeeUpdateDTO;
import com.sz.admin.sysuseremployee.pojo.dto.SysUserEmployeeListDTO;
import com.sz.admin.sysuseremployee.pojo.dto.SysUserEmployeeImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.sysuseremployee.pojo.vo.SysUserEmployeeVO;

/**
 * <p>
 * 员工用户表 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-09-04
 */
@Service
@RequiredArgsConstructor
public class SysUserEmployeeServiceImpl extends ServiceImpl<SysUserEmployeeMapper, SysUserEmployee> implements SysUserEmployeeService {
    @Override
    public void create(SysUserEmployeeCreateDTO dto){
        SysUserEmployee sysUserEmployee = BeanCopyUtils.copy(dto, SysUserEmployee.class);
        save(sysUserEmployee);
    }

    @Override
    public void update(SysUserEmployeeUpdateDTO dto){
        SysUserEmployee sysUserEmployee = BeanCopyUtils.copy(dto, SysUserEmployee.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(SysUserEmployee::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(sysUserEmployee);
    }

    @Override
    public PageResult<SysUserEmployeeVO> page(SysUserEmployeeListDTO dto){
        Page<SysUserEmployeeVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), SysUserEmployeeVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<SysUserEmployeeVO> list(SysUserEmployeeListDTO dto){
        return listAs(buildQueryWrapper(dto), SysUserEmployeeVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public SysUserEmployeeVO detail(Object id){
        SysUserEmployee sysUserEmployee = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(sysUserEmployee);
        return BeanCopyUtils.copy(sysUserEmployee, SysUserEmployeeVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<SysUserEmployeeImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), SysUserEmployeeImportDTO.class, true);
        List<SysUserEmployeeImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(SysUserEmployeeListDTO dto, HttpServletResponse response) {
        List<SysUserEmployeeVO> list = list(dto);
        String fileName = "员工用户表模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "员工用户表", SysUserEmployeeVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(SysUserEmployeeListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SysUserEmployee.class);
        if (Utils.isNotNull(dto.getUsername())) {
            wrapper.like(SysUserEmployee::getUsername, dto.getUsername());
        }
        if (Utils.isNotNull(dto.getPwd())) {
            wrapper.eq(SysUserEmployee::getPwd, dto.getPwd());
        }
        if (Utils.isNotNull(dto.getPhone())) {
            wrapper.eq(SysUserEmployee::getPhone, dto.getPhone());
        }
        if (Utils.isNotNull(dto.getNickname())) {
            wrapper.like(SysUserEmployee::getNickname, dto.getNickname());
        }
        if (Utils.isNotNull(dto.getSex())) {
            wrapper.eq(SysUserEmployee::getSex, dto.getSex());
        }
        if (Utils.isNotNull(dto.getBirthday())) {
            wrapper.eq(SysUserEmployee::getBirthday, dto.getBirthday());
        }
        if (Utils.isNotNull(dto.getLogo())) {
            wrapper.eq(SysUserEmployee::getLogo, dto.getLogo());
        }
        if (Utils.isNotNull(dto.getAge())) {
            wrapper.eq(SysUserEmployee::getAge, dto.getAge());
        }
        if (Utils.isNotNull(dto.getIdCard())) {
            wrapper.eq(SysUserEmployee::getIdCard, dto.getIdCard());
        }
        if (Utils.isNotNull(dto.getEmail())) {
            wrapper.eq(SysUserEmployee::getEmail, dto.getEmail());
        }
        if (Utils.isNotNull(dto.getAccountStatusCd())) {
            wrapper.eq(SysUserEmployee::getAccountStatusCd, dto.getAccountStatusCd());
        }
        if (Utils.isNotNull(dto.getUserTagCd())) {
            wrapper.eq(SysUserEmployee::getUserTagCd, dto.getUserTagCd());
        }
        if (Utils.isNotNull(dto.getLastLoginTimeStart()) && Utils.isNotNull(dto.getLastLoginTimeEnd())) {
            wrapper.between(SysUserEmployee::getLastLoginTime, dto.getLastLoginTimeStart(), dto.getLastLoginTimeEnd());
        }
        return wrapper;
    }
}